import re

from django.contrib.auth import authenticate


from django.contrib.auth.backends import ModelBackend
import logging

from apps.users.models import User
# from django.contrib.auth.models import User

logger=logging.getLogger('django')


class UsernameMobileModelBackend(ModelBackend):
    def authenticate(self,request,username=None,password=None,**kwargs):
        try:
            if re.match(r'1[3-9]\d{9}',username):
    ##########注意点User导入包不要导错了
    ########### User是models表中的自己重新定义的方法
                user = User.objects.get(mobile=username)
            else:
                user = User.objects.get(username=username)
        except Exception as e:
            logger.error(e)
            return None
        else:
            if user.check_password(password):
                return user



